package nine.test_9_5;

import java.util.Scanner;

public class TestMain1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int x = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(getMin(n,x,arr));
    }
    private static int getMin(int n, int x,int[] arr) {
        int[] dp = new int[x];
        for (int i = 0; i < x; i++) {
            dp[i] = Integer.MIN_VALUE;
        }
        for (int a : arr) {
            int[] newDp = dp.clone();
            for (int j = 0; j < x; j++) {
                newDp[j] = Math.min(newDp[j],dp[(j - a + x) % x]);
                newDp[j] = Math.min(newDp[j],dp[(j - (a + 1) + x) % x]);
            }
            dp = newDp;
        }
        return dp[0] == Integer.MIN_VALUE / 2 ? -1 : dp[0];
    }
}
